package com.lb.jdbc;

import com.lb.jdbc.entity.Emp;


import java.sql.*;


public class Test {
    private int id;
    private String name;
    private String age;
    private String password;
    private String createTime;
    public Test(){}

    @Override
    public String toString() {
        return "Test{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", password='" + password + '\'' +
                ", createTime='" + createTime + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }


    public static void main(String[] args) throws Exception {
//        selectAll();
//        selectByName1("'李白'");
//        selectByName2("李白");
          //String sql = "";
          //selectBySql();

    }

    /**
     * 通过名字查询
     */
    private static void selectByName1(String name) throws Exception {
        //定义sql语句
        String sql = "select * from user where name=" + name;

        Connection connection = JdbcUtils.getCon();

        //获取sql语句的对象
        Statement statement = connection.createStatement();
        //执行我的sql语句
        ResultSet rs = statement.executeQuery(sql);

        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }
        JdbcUtils.close(statement, connection);

    }

    /**
     * 通过名字查询
     */
    private static void selectByName2(String name) throws Exception {
        //定义sql语句
        String sql = "select * from user where name=?";

        Connection connection = JdbcUtils.getCon();
        //获取sql语句的对象
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, name);
        //执行我的sql语句
        ResultSet rs = preparedStatement.executeQuery();

        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }

        JdbcUtils.close(preparedStatement, connection);

    }

    /**
     * 通过sql查询
     */
    private static void selectBySql() throws Exception {
        Connection connection = JdbcUtils.getCon();
        //定义sql语句
        String sql = "select * from `user` where age>'20'";
        Statement statement = connection.createStatement();
        //执行我的sql语句
        ResultSet rs = statement.executeQuery(sql);

        while (rs.next()) {
            int id = rs.getInt(1);
            String name = rs.getString(2);
            String age = rs.getString(3);
            String password = rs.getString(4);
            float create_time = rs.getLong(5);
            //输出结果
            System.out.println("id" + id + " name" + name + " age" + age + " password" + password + " create_time" + create_time);
            System.out.println("................................................");
        }
        JdbcUtils.close(statement, connection);
    }




    /**
     * 通过实体查询
     * @param emp
     */

    private static void selectByEntity(Emp emp) {

    }




         /**
          * 查询所有
          *
          * @throws Exception
          */
         private static void selectAll () throws Exception {
             Connection connection = JdbcUtils.getCon();
             //获取sql语句的对象
             Statement statement = connection.createStatement();
             //4.定义sql语句
             String sql = "select * FROM emp";
             //5.执行我的sql语句
             ResultSet rs = statement.executeQuery(sql);
             while (rs.next()) {
                 int empno = rs.getInt(1);
                 String ename = rs.getString(2);
                 String job = rs.getString(3);
                 Date hiredate = rs.getDate(4);
                 float sal = rs.getFloat(5);
                 //输出结果
                 System.out.println("enempno=" + empno + " ame=" + ename + " job=" + job + " hiredate=" + hiredate + " sal" + sal);
                 System.out.println("................................................");
             }
             JdbcUtils.close(statement, connection);
         }
     }
